<?php
namespace app;

use libs\qoy\BaseController;
use libs\qoy\DI;
use libs\qoy\Session;

class Index extends BaseController
{

    public function getRules()
    {}

    // "state" => array(
    // "id" => array(
    // "name" => "id",
    // "type" => "int",
    // "default" => 1
    // ),
    // "openid" => array(
    // "name" => "openid",
    // "type" => "string",
    // "require" => true
    // )
    // ),
    public function getCode()
    {
        error_reporting(0);
        Session::init();
        if (! Session::has("id")) {
            $maxid = DI::instance()->cache->get("maxid");
            $id = $maxid + 1;
            DI::instance()->cache->set("maxid", $id, 31536000);
            $id = self::numl($id, 4);
            Session::set("id", $id);
        } else {
            $id = Session::get("id");
        }
        return array(
            "id" => $id
        );
    }

    public function upphoto()
    {
        if ($_POST["code"]) {
            if ($_FILES["file"]["type"] == "image/jpeg" && $_FILES["file"]["size"] < 2048 * 1024) {
                move_uploaded_file($_FILES["file"]["tmp_name"], "./../public/uploads/" . $_POST["code"] . ".jpg");
                $msg = "图片上传成功";
            } else {
                echo $_FILES["file"]["size"];
                $msg = "请上传jpg格式图片且大小<2M";
            }
        } else {
            $msg = "请输入用户code";
        }
        
        $html = '<html><head> <meta http-equiv="refresh" content="5; URL=/upphoto.html"> </head> <body>' . $msg . '</body> </html> ';
        echo $html;
        exit();
    }

    static function numl($v, $len)
    {
        $str = $v . "";
        while (strlen($str) < 4) {
            $str = "0" . $str;
        }
        return $str;
    }
}
